<?php
class RelatestudentController extends MyClass_Action {
	protected $controller;
	protected $action;
	protected $_user;
	
	public function init() {
		$this->commonAction();	
	}
       
	public function indexAction(){
		try {
		    $sql = "select DISTINCT(p1.pid),p1.realname,p1.mobile,s.cnname
from parents p1, student s where p1.pid=s.pid  and p1.mobile<>''  and s.inactive=0 order by p1.mobile";
			$result = $this->_dbAdapter->fetchAll($sql);   
			$this->view->result = $result; 
			
		} catch ( Exception $e ) {
				$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	public function togetherAction() {
		try {
                  $pid = $this->_request->getParam('pid');
                  $sql = "select pid from parents where mobile =(select mobile from parents where pid=".$pid.")";
                  $data = $this->_dbAdapter->fetchAll($sql);
                  foreach($data as $da){
                      if($da['pid']!=$pid){
                         $pids[] = $da['pid'];
                      }
                  }
                  $pidstr = implode(',',$pids);
                   $sql = "update student set pid=".$pid." where uid in (".$pidstr.")";
                   $this->_dbAdapter->query($sql);
                   $sql = "delete from parents where pid in(".$pidstr.")";
                   $this->_dbAdapter->query($sql);	
				  
                  $url = '/' . $this->controller . '/index';
                  $this->_redirect ( $url );
		} catch ( Exception $e ){
		    $this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
        //机构查重
	public function ckstudentAction() {
		try {
              $sql = "select s1.uid,s1.cnname
from student s1,student s2 where s1.uid<>s2.uid and s1.cnname=s2.cnname";              
              $result = $this->_dbAdapter->fetchAll($sql);
              $this->_view->data=$result;                  
		} catch ( Exception $e ){
		    $this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
    //关联班级
	public function indexbAction(){
	    try {
           $sql = "select uid,cnname,count(*) as num from student where cnname<>'' group by cnname  HAVING num>1";
			$result = $this->_dbAdapter->fetchAll($sql);   
			$this->view->result = $result; 			
           // print_r($result);exit();      
		} catch ( Exception $e ){
		    $this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	//合并student_class表里的学生uid，更新etonkis表里的uid
	public function togetherbAction() {
		try {
                  $uid = $this->_request->getParam('uid');
                  $sql = "select uid from student where cnname =(select cnname from student where uid=".$uid.")";
                  $data = $this->_dbAdapter->fetchAll($sql);
                  foreach($data as $da){
                      if($da['uid']!=$uid){
                         $uids[] = $da['uid'];
                      }
                  }	
                  $uidstr = implode(',',$uids);
				 
				  //删除student表里的重名
				  $sql = "delete from student where uid in(".$uidstr.")";
				  $this->_dbAdapter->query($sql);
				  //更新student_class里的student_uid
				  foreach($uids as $id){
					$sql = "select 1 from student_class where uid_student=".$id." and uid_class =(select uid_class from student_class where uid_student =".$uid.")";
					$row = $this->_dbAdapter->query($sql);
					if(!empty($row)){
					     $this->_dbAdapter->query("delete from student_class where uid_student=".$id);	
					}else{
					     $this->_dbAdapter->query("update student_class set uid_student=".$uid." where uid_student =".$id);
						 $this->_dbAdapter->query("update etonkids.student set uid=".$uid." where uid =".$id);	
					}
				  }
                  $url = '/' . $this->controller . '/indexb';
                  $this->_redirect ( $url );
		} catch ( Exception $e ){
		    $this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
     
	public function __call($method, $args) {
		if ('Action' == substr ( $method, - 6 )) {
			$url = '/' . $this->controller . '/index';
			return $this->_redirect ( $url );
		}
		throw new Exception ( 'Invalid method' );
	}
}
?>
